﻿using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;
using System.Linq;

public class NewBehaviourScript : MonoBehaviour
{
    IList<ICanvasElement> m_LayoutRebuildQueue;
    IList<ICanvasElement> m_GraphicRebuildQueue;

    void Start()
    {
        Test test = new Test();
        test.Main();
        System.Type type = typeof(CanvasUpdateRegistry);
        FieldInfo field = type.GetField("m_LayoutRebuildQueue", BindingFlags.NonPublic | BindingFlags.Instance);
        m_LayoutRebuildQueue = (IList<ICanvasElement>)field.GetValue(CanvasUpdateRegistry.instance);
        field = type.GetField("m_GraphicRebuildQueue", BindingFlags.NonPublic| BindingFlags.Instance);
        m_GraphicRebuildQueue = (IList<ICanvasElement>)field.GetValue(CanvasUpdateRegistry.instance);
    }

    void Update()
    {
        for (int i = 0; i < m_LayoutRebuildQueue.Count; i++)
        {
            var rebuild = m_LayoutRebuildQueue[i];
            if (ObjectValidForUpdate(rebuild))
            {
                Debug.Log($"{rebuild.transform.name}引起网格重建");
            }
        }
        for (int i = 0; i < m_GraphicRebuildQueue.Count; i++)
        {
            var element = m_GraphicRebuildQueue[i];
            if (ObjectValidForUpdate(element))
                Debug.Log($"{element.transform.name}引起{element.transform.GetComponent<Graphic>().canvas.name}网格重建");
        }
    }
    private bool ObjectValidForUpdate(ICanvasElement element)
    {
        var valid = element is Object;
        var isUnityObject = element is Object;
        if(isUnityObject)
            valid = (element as Object) != null;
        return valid;
    }
}
public class Test
{
    int num = 0;
    int up = 0;
    int down = 0;
    int[] arr = { 1, -2 };//3 7
    public void Main()
    {
        for (int i = 0; i < arr.Length; i++)
        {
           if(arr[i] >= 0)
                up++;
           else
                down++;
            num += Mathf.Abs(arr[i]);
        }
        int a = 3; int b = -5;
        Debug.Log(num);
        AddAll(-3);
        Debug.Log(num);
        AddAll(3);
    }
    void AddAll(int num)
    {
        if(num >= 0)
        {
            this.num += down * num;
            this.num -= up * num;
        }
        else
        {
            this.num -= down * num;
            this.num += up * num;
        }
        down = 0; up = 0;
        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] += num;
            if (arr[i] >= 0)
                up++;
            else
                down++;
        }
    }
}